var express = require('express');
var router = express.Router();
var https = require('https');
var util = require('util');
var superagent = require('superagent');
const station_name_url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9064';
let url = 'https://kyfw.12306.cn/otn/leftTicket/queryA?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT';



/* GET home page. */
router.get('/x', function (req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});

router.get('/', function (req, res, next) {
	res.render('ticketResult');
});

router.post('/search', function (req, ares, next) {
	let from_station_name = req.body.fromStation;
	let to_station_name = req.body.toStation;
	let train_date = req.body.trainDate;

	https.get(station_name_url, function (res) {
		var result = '';
		res.on('data', function (chunk) {
			result += chunk;
		});
		res.on('end', function () {
			let from_station = '';
			let to_station = '';
			result = result.toString('utf8');
			let l = result.split('@');
			let promise = new Promise(function (resolve, reject) {
				let i = 1;
				for (i = 1; i < l.length; i++) {
					var tmp = l[i].split('|')[1];

					if (tmp === from_station_name) {
						from_station = l[i].split('|')[2].toUpperCase();
					}
					if (tmp === to_station_name) {
						to_station = l[i].split('|')[2].toUpperCase();
					}
				}
				
				var result = {
					from_station: from_station,
					to_station: to_station
				}
				console.log(result);
				if(i >= l.length){
					resolve(result);
					console.log(result);
				}

			});

			let search_url = '';
			
			promise.then(function (result) {
				search_url = util.format(url, train_date, result.from_station, result.to_station);
				console.log(search_url);
				superagent.get(search_url)
					.end(function(err, result){
						let chunks = result.text;
						console.log('fuck json parse');
						console.log('chunks is '+chunks);
						let train_info = JSON.parse(chunks);
						let train_list = train_info.data.result;
						let final_result = [];
						
						train_list.forEach((element, index) => {
							let list = {};
							let one_train_info = element.split('|');
							let start_time = one_train_info[8];
							let end_time = one_train_info[9];
							let train = one_train_info[3];
							let train_no = one_train_info[2];
							let train_consuming = one_train_info[10];
							let A9 = one_train_info[32];//商特
							let A4 = one_train_info[23];//软卧
							let A6 = one_train_info[21];//高卧
							let M = one_train_info[31];//一等座
							let O = one_train_info[30];//二等座
							let P = one_train_info[25];//商特
							let A3 = one_train_info[28];//硬卧
							let A1 = one_train_info[29];//硬座
							let WZ = one_train_info[26];//无座
							let F = one_train_info[33];//动卧
							list.start_time = start_time;
							list.end_time = end_time;
							list.train = train;
							list.train_consuming = train_consuming;
							list.A9 = A9;
							list.A4 = A4;
							list.A6 = A6;
							list.M = M;
							list.O = O;
							list.P = P;
							list.A3 = A3;
							list.A1 = A1;
							list.WZ = WZ;
							list.F = F;
							final_result.push(list);
						});
						ares.send(final_result);
					});
			});

		});

	});
});



module.exports = router;